// 获取今日时间
export function getToday() {
  const date = new Date()
  const seperator1 = '-'
  const year = date.getFullYear()
  let month = date.getMonth() + 1
  let strDate = date.getDate()
  if (month >= 1 && month <= 9) {
    month = '0' + month
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = '0' + strDate
  }
  const currentdate = year + seperator1 + month + seperator1 + strDate
  return currentdate
}

// time字符串转时间戳

export function strToTime(time) {
  time = time.replace(/-/g, '/')
  return Date.parse(time)
}
export function beforeDate(date, num) {
  var timestamp = date.getTime()
  return new Date(timestamp + num * 1000 * 60 * 60 * 24)
}
// 获取xx天后的日期 传入负数 则是多少天前的日期
/**
 * @param {Object} date  日期字符串  yyyy-MM-dd
 * @param {Object} num   多少天  数字
 */
export function getBeforeDate(date, num) {
  var timestamp = strToTime(date)
  return new Date(timestamp + num * 1000 * 60 * 60 * 24)
}
/**
 * @param {Object} value Date对象
 * @param {Object} fmt
 */
export function formatDate(value, fmt = 'yyyy-MM-dd') {
  const getDate = new Date(value)
  const o = {
    'M+': getDate.getMonth() + 1,
    'd+': getDate.getDate(),
    'h+': getDate.getHours(),
    'm+': getDate.getMinutes(),
    's+': getDate.getSeconds(),
    'q+': Math.floor((getDate.getMonth() + 3) / 3),
    'S': getDate.getMilliseconds()
  }
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (getDate.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (const k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return fmt
}

/* 计算两个时间的天数 */
export function getAllDate(start, end, isLength = true) {
  const dateArr = []
  const startArr = start.split('-')
  const endArr = end.split('-')
  const db = new Date()
  db.setUTCFullYear(startArr[0], startArr[1] - 1, startArr[2])
  const de = new Date()
  de.setUTCFullYear(endArr[0], endArr[1] - 1, endArr[2])
  const unixDb = db.getTime()
  const unixDe = de.getTime()
  let stamp
  const oneDay = 24 * 60 * 60 * 1000
  for (stamp = unixDb; stamp <= unixDe;) {
    dateArr.push(formatDate(new Date(parseInt(stamp))))
    stamp = stamp + oneDay
  }
  return isLength ? dateArr.length : dateArr
}
// 字符串转Date
export function str2Date(dateString) {
  return new Date(dateString.replace(/-/g, '/'))
}

// 字符串转换为时间戳
export function getDateTimeStamp(dateStr) {
  return Date.parse(dateStr.replace(/-/gi, '/'))
}

export function getDateDiff(dateStr) {
  const publishTime = getDateTimeStamp(dateStr) / 1000
  let d_seconds
  let d_minutes
  let d_hours
  let d_days
  const timeNow = parseInt(new Date().getTime() / 1000)
  let d

  const date = new Date(publishTime * 1000)
  const Y = date.getFullYear()
  let M = date.getMonth() + 1
  let D = date.getDate()
  let H = date.getHours()
  let m = date.getMinutes()
  let s = date.getSeconds()
  // 小于10的在前面补0
  if (M < 10) {
    M = '0' + M
  }
  if (D < 10) {
    D = '0' + D
  }
  if (H < 10) {
    H = '0' + H
  }
  if (m < 10) {
    m = '0' + m
  }
  if (s < 10) {
    s = '0' + s
  }

  // eslint-disable-next-line prefer-const
  d = timeNow - publishTime
  // eslint-disable-next-line prefer-const
  d_days = parseInt(d / 86400)
  // eslint-disable-next-line prefer-const
  d_hours = parseInt(d / 3600)
  // eslint-disable-next-line prefer-const
  d_minutes = parseInt(d / 60)
  // eslint-disable-next-line prefer-const
  d_seconds = parseInt(d)

  if (d_days > 0 && d_days < 3) {
    return d_days + '天前'
  } else if (d_days <= 0 && d_hours > 0) {
    return d_hours + '小时前'
  } else if (d_hours <= 0 && d_minutes > 0) {
    return d_minutes + '分钟前'
  } else if (d_seconds < 60) {
    if (d_seconds <= 0) {
      return '刚刚发表'
    } else {
      return d_seconds + '秒前'
    }
  } else if (d_days >= 3 && d_days < 30) {
    return M + '-' + D + ' ' + H + ':' + m
  } else if (d_days >= 30) {
    return Y + '-' + M + '-' + D + ' ' + H + ':' + m
  }
}

export function currentTime() {
  const now = new Date()

  const year = now.getFullYear()
  const month = now.getMonth() + 1
  const day = now.getDate()

  const hh = now.getHours()
  const mm = now.getMinutes()
  const ss = now.getSeconds()

  let clock = year + '-'

  if (month < 10) {
    clock += '0'
  }
  clock += month + '-'
  if (day < 10) {
    clock += '0'
  }
  clock += day + ' '
  if (hh < 10) {
    clock += '0'
  }
  clock += hh + ':'
  if (mm < 10) clock += '0'
  clock += mm + ':'
  if (ss < 10) clock += '0'
  clock += ss
  return (clock)
}

export function getDateRangeDiff(start, end, num) {
  const diff = end.getTime() - start.getTime()
  return num <= Math.floor((diff / 1000) % 60)
}

/**
 * 根据传递的日期返回日期中这个月的第一天和最后一天
 * @param {Object} date
 */
export function getMonthStart_End(date) {
  const obj = {}
  const year = date.split('-')[0]
  const month = date.split('-')[1]

  const d = new Date(date)
  // 将日期设置为下月一号
  d.setMonth(d.getMonth() + 1)
  d.setDate('1')
  // 获取本月最后一天
  d.setDate(d.getDate() - 1)

  obj.start = year + '-' + month + '-' + '01'
  obj.end = year + '-' + month + '-' + d.getDate()

  return obj
}
// 获取当月第一天
export function getFirstDayOfMonth(date) {
  date.setDate(1)
  return date
}

// 获取本月最后一天
export function currentMonthLast(date) {
  var currentMonth = date.getMonth()
  var nextMonth = ++currentMonth
  var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1)
  var oneDay = 1000 * 60 * 60 * 24
  return new Date(nextMonthFirstDay - oneDay)
}
// 判断两个日期相差的天数
export function getDaysByDateString(dateString1, dateString2) {
  var startDate = Date.parse(dateString1.replace('/-/g', '/'))
  var endDate = Date.parse(dateString2.replace('/-/g', '/'))
  var diffDate = (endDate - startDate) + 1 * 24 * 60 * 60 * 1000
  var days = diffDate / (1 * 24 * 60 * 60 * 1000)
  return days
}
// 获取本月有多少天
export function getMonthDay(year, month) {
  const days = new Date(year, month + 1, 0).getDate()
  return days
}

/**
 * 判断一个时间是否是当天
 * @param {Object} date
 */
export function isToday(date) {
  var d = new Date(date.toString().replace(/-/g, '/'))
  var todaysDate = new Date()
  return d.setHours(0, 0, 0, 0) === todaysDate.setHours(0, 0, 0, 0)
}
/**
 * 判断是否是当前年
 * @param {Object} date
 */
export function isSameYear(date) {
  var d = new Date(date.toString().replace(/-/g, '/'))
  var todaysDate = new Date()
  return d.getFullYear() === todaysDate.getFullYear()
}

// 补零函数
function doHandleZero(zero) {
  var date = zero
  if (zero.toString().length === 1) {
    date = '0' + zero
  }
  return date
}
// 获取指定日期的一号
export function getCurrentMonthDay(date) {
  var myDate = new Date(date)
  var tYear = myDate.getFullYear()
  var tMonth = myDate.getMonth()
  tMonth = doHandleZero(tMonth + 1)

  return tYear + '-' + tMonth + '-01'
}

/**
 * 计算两时间的差
 * @param {Object} date1
 * @param {Object} date2
 * @param {Object} type		返回单个还是多个
 */
export function diffTime(date1, date2, type) {
  date1 = typeof date1 === 'object' ? date1 : date1.replace(/\-/g, '/') // 开始时间
  date2 = typeof date2 === 'object' ? date2 : date2.replace(/\-/g, '/') // 结束时间
  var date3 = new Date(date2).getTime() - new Date(date1).getTime() // 时间差的毫秒数

  // 计算出相差天数
  var days = Math.floor(date3 / (24 * 3600 * 1000))

  // 计算出小时数
  var leave1 = date3 % (24 * 3600 * 1000) // 计算天数后剩余的毫秒数
  var hours = Math.floor(leave1 / (3600 * 1000))
  // 计算相差分钟数
  var leave2 = leave1 % (3600 * 1000) // 计算小时数后剩余的毫秒数
  var minutes = Math.floor(leave2 / (60 * 1000))
  // 计算相差秒数
  var leave3 = leave2 % (60 * 1000) // 计算分钟数后剩余的毫秒数
  var seconds = Math.round(leave3 / 1000)

  // 不足1分钟
  if (!days && !hours && !minutes) return seconds + '秒'
  // 不足1小时
  if (!days && !hours) return type === 'multiple' ? minutes + '分钟' + seconds + '秒' : minutes + '分钟'
  // 不足一天
  if (!days) return type === 'multiple' ? hours + '时' + minutes + '分钟' : hours + '时'
  // 大于一天
  return type === 'multiple' ? days + '天' + hours + '时' + minutes + '分钟' : days + '天'
}

/**
 * 根据所给日期计算距离今天的天数
 * @param {Object} date
 */
export function daysDistance(date) {
  var time = Date.parse(new Date())
  var lasttime = Date.parse(date.replace(/-/g, '/'))
  return	parseInt((time - lasttime) / (1000 * 60 * 60 * 24))
}

/* 根据当前时间  判断是否包含在开始日期和结束日期中 */
export function nowInDateBetwen(d1, d2, now) {
  var dateBegin = new Date(d1)// 将-转化为/，使用new Date
  var dateEnd = new Date(d2)// 将-转化为/，使用new Date
  var dateNow = new Date(now)// 获取当前时间

  var beginDiff = dateNow.getTime() - dateBegin.getTime()// 时间差的毫秒数
  var beginDayDiff = Math.floor(beginDiff / (24 * 3600 * 1000))// 计算出相差天数

  var endDiff = dateEnd.getTime() - dateNow.getTime()// 时间差的毫秒数
  var endDayDiff = Math.floor(endDiff / (24 * 3600 * 1000))// 计算出相差天数
  if (endDayDiff < 0) { // 已过期
    return false
  }
  if (beginDayDiff < 0) { // 没到开始时间
    return false
  }
  return true
}

// 距离今天多少年多少月
export function monthDayDiff(startDate) {
  // this指针
  const flag = [1, 3, 5, 7, 8, 10, 12, 4, 6, 9, 11, 2]
  const start = new Date(formatDate(startDate))
  const end = new Date()
  let year = end.getFullYear() - start.getFullYear()
  let month = end.getMonth() - start.getMonth()
  let day = end.getDate() - start.getDate()
  if (month < 0) {
    year--
    month = end.getMonth() + (12 - start.getMonth())
  }
  if (day < 0) {
    month--
    const index = flag.findIndex((temp) => {
      return temp === start.getMonth() + 1
    })
    let monthLength
    if (index <= 6) {
      monthLength = 31
    } else if (index > 6 && index <= 10) {
      monthLength = 30
    } else {
      monthLength = 28
    }
    day = end.getDate() + (monthLength - start.getDate())
  }
  // this.result = `相差${12 * year + month}月${day}天`;
  return `${year ? year + '年' : ''}${month > 0 ? month + '个月' : ''}`
}
